import java.util.*;

public class newtest{
    static HashMap<Integer,List<Integer>> map = new HashMap();
    static int res = 0;
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] nums = new int[n];
        for(int i=1;i<n;i++){
            nums[i] = in.nextInt();
        }
        for(int i=1;i<nums.length;i++){
            List list = map.getOrDefault(nums[i],new ArrayList());
            list.add(i+1);
            map.put(nums[i],list);
        }
        Node node = new Node(1);
        buildTree(node);
        getHeight(node,0);
        int leftHeight = node.lefthegiht;
        int rightHeight = node.rightheight;
        getRes(node.right,leftHeight,rightHeight);
        getRes(node.left,rightHeight,leftHeight);
        System.out.print(res);
    }
    static void buildTree(Node node){
        if(node==null||!map.containsKey(node.val))return;
        List<Integer> list = map.get(node.val);
        if(list.size()==1){
            node.left = new Node(list.remove(0));
        }else{
            node.left = new Node(list.remove(0));
            node.right = new Node(list.remove(0));
        }
        buildTree(node.left);
        buildTree(node.right);
    }
    static int getHeight(Node node,int height){
        if(node==null)return height;
        int left = getHeight(node.left,height);
        node.lefthegiht = left;
        int right = getHeight(node.right,height);
        node.rightheight = right;
        node.height = Math.max(left,right);
        return Math.max(left,right)+1;
    }
    static void getRes(Node node,int longest,int curheight){
        if(node==null)return;
        int res1 = (node.lefthegiht+node.rightheight+longest+curheight-node.height)*2;
        int res2 = (res1/2-Math.min(Math.min(node.lefthegiht,node.rightheight),curheight-node.height))*2;
        int res3 = node.height*2;
        res = Math.max(res,Math.max(res1,Math.max(res2,res3)));
        getRes(node.left,longest,curheight);
        getRes(node.right,longest,curheight);
    }
}
class Node{
    Node left;
    Node right;
    int val;
    int height;
    int lefthegiht;
    int rightheight;
    Node(int val){
        this.val = val;
    }
}